package org.xtb.weatherQuery;

import javax.xml.namespace.QName;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Objects;
import java.util.Scanner;
import javax.xml.soap.*;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

public class WeatherQuery {
    public static void main(String[] args) {
        try {
            // 创建URL对象，指定Web Service的地址
            URL url = new URL("http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl");

            // 创建SOAPMessage对象
            SOAPMessage soapMessage = MessageFactory.newInstance(SOAPConstants.SOAP_1_1_PROTOCOL).createMessage();

            // 创建SOAPPart对象
            SOAPPart soapPart = soapMessage.getSOAPPart();

            // 创建SOAPEnvelope对象
            soapMessage.getSOAPPart().getEnvelope().setPrefix("soapenv");
            soapMessage.getSOAPBody().setPrefix("web");

            // 创建QName对象，指定命名空间和方法名
            QName qname = new QName("http://WebXml.com.cn/", "getWeatherbyCityName");

            Scanner sc = new Scanner(System.in);

            System.out.println("请输入查询城市：");
            String next = sc.next();

            // 创建SOAPBody对象，并添加方法参数
            soapMessage.getSOAPBody().addBodyElement(qname).addChildElement("theCityName").setTextContent(next);

            // 发送SOAP请求并获取响应
            SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
            SOAPConnection soapConnection = soapConnectionFactory.createConnection();
            SOAPMessage response = soapConnection.call(soapMessage, url);

            // 解析响应数据
            SOAPBody soapBody = response.getSOAPBody();
            Document doc = soapBody.extractContentAsDocument();

            // 获取返回数据的节点
            NodeList nodeList = doc.getElementsByTagName("string");

            // 解析返回数据
            if (nodeList.getLength() > 0) {
                Element province = (Element) nodeList.item(0);
                Element cityName = (Element) nodeList.item(1);
                Element lastTime = (Element) nodeList.item(4);
                Element qiwen = (Element) nodeList.item(5);
                Element gaikuang = (Element) nodeList.item(6);
                Element fengli = (Element) nodeList.item(7);
                Element qiwen2 = (Element) nodeList.item(12);
                Element gaikuang2 = (Element) nodeList.item(13);
                Element fengli2 = (Element) nodeList.item(14);
                String province1 = province.getTextContent();
                String cityName1 = cityName.getTextContent();
                String lastTime1 = lastTime.getTextContent();
                String qiwen1 = qiwen.getTextContent();
                String gaikuang1 = gaikuang.getTextContent();
                String fengli1 = fengli.getTextContent();
                String qiwen3 = qiwen2.getTextContent();
                String gaikuang3 = gaikuang2.getTextContent();
                String fengli3 = fengli2.getTextContent();
                // 打印天气预报信息
                System.out.println("省份：" + province1);
                System.out.println("城市：" + cityName1);
                System.out.println("最后发布时间：" + lastTime1);
                System.out.println("-----------------");
                System.out.println("--------当天：---------");
                System.out.println("气温：" + qiwen1);
                System.out.println("概况：" + gaikuang1);
                System.out.println("风力风向：" + fengli1);
                System.out.println("-----------------");
                System.out.println("---------第二天：--------");
                System.out.println("气温：" + qiwen3);
                System.out.println("概况：" + gaikuang3);
                System.out.println("风力风向：" + fengli3);
            }

            // 关闭SOAP连接
            soapConnection.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
